package com.xoa.service.party;

import com.xoa.dao.department.DepartmentMapper;
import com.xoa.dao.footprint.FootPrintMapper;
import com.xoa.dao.party.GridEventMapper;
import com.xoa.dao.party.GridEveromentUserMapper;
import com.xoa.dao.party.GridHolidayMapper;
import com.xoa.dao.party.OrgGovimentMapper;
import com.xoa.model.department.Department;
import com.xoa.model.footprint.Foot;
import com.xoa.model.party.GridEveromentUser;
import com.xoa.model.party.GridHoliday;
import com.xoa.model.party.OrgGoviment;
import com.xoa.model.users.Users;
import com.xoa.util.DateFormat;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created by gsb on 2018/1/12.
 */
@Service
public class GridEveromentUserService {

    @Resource
    private GridEveromentUserMapper gridEveromentUserMapper;

    @Resource
    private GridHolidayMapper gridHolidayMapper;

  @Resource
  private FootPrintMapper footPrintMapper;

  @Resource
  private OrgGovimentMapper orgGovimentMapper;
  @Resource
  private DepartmentMapper departmentMapper;

  @Resource
  private GridEventMapper gridEventMapper;

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-12 下午15:26:51
     * 类介绍  :  网格员签到情况
     *   key=0  为周末   key=1 为工作日
     * 构造参数:
     *
     */
    public ToJson<GridEveromentUser> select(String time, String key, Integer deptId, Integer page, Integer pageSize,
                                            boolean useFlag, String isPage){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            Integer begin = DateFormat.getDateTime(time);// 开始时间戳
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(time);
            if(time.equals(DateFormat.getCurrentTime2())){
                List<GridEveromentUser> gridEveromentUserList = new ArrayList<>();
                Calendar calendar  =  new GregorianCalendar();
                calendar.setTime(date);
                calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
                date=calendar.getTime();   //这个时间就是日期往后推一天的结果
                String T = DateFormat.getDatestr(date);
                Integer end = DateFormat.getDateTime(T); // 结束时间戳
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("begin",begin);
                map.put("end",end);
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("newTime",time);
                 //查询前端传入时间是否在节假日范围内
                List<GridHoliday> gridHolidayList =gridHolidayMapper.selectHoliday(map1);
                GridEveromentUser gridEveromentUser = null;
                Date date1=sdf.parse(time);

                List<Users> usersList = gridEveromentUserMapper.selectUser(deptId);
                if (usersList!=null && usersList.size()>0){
                    for (Users user:usersList){
                        gridEveromentUser = new GridEveromentUser();
                        gridEveromentUser.setGridId(user.getDeptId().toString());
                        gridEveromentUser.setGridName(user.getDeptName());
                        gridEveromentUser.setGridUserId(user.getUserId());
                        gridEveromentUser.setGridUserName(user.getUserName());
                        gridEveromentUser.setDate(date1);
                        map.put("uid",user.getUid());
                      /*  List<Foot> footList = footPrintMapper.getUserFootPrint(map);
                        //判断 输入时间  那天  有无签到
                        if (footList!=null && footList.size()>0){
                            Integer fTime=footList.get(0).getUpdateTime();
                            Long l=(new Long(fTime.toString()))*1000; ;
                            Date d = new Date(l);
                            String res = sdf.format(d);
                            gridEveromentUser.setAttendNum(res);
                            gridEveromentUser.setAttendScore(footList.get(0).getLocation());
                            if("0".equals(key)){
                                gridEveromentUser.setM1("0");
                            }else if (gridHolidayList !=null && gridHolidayList.size()>0){
                                gridEveromentUser.setM1("0");
                            }else {
                                gridEveromentUser.setM1("1");
                            }
                        }else {
                            gridEveromentUser.setM1("0");
                        }*/
                        gridEveromentUserList.add(gridEveromentUser);
                       /* Map<String, Object> map2 = new HashMap<String, Object>();
                        map2.put("date",date1);
                        map2.put("gridUserId",user.getUserId());
                        //  查询输入时间  是否在环保考核人员表里有记录
                        //有就执行更新操作  没有就新建
                        List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map2);
                        if(gridEveromentUserList!=null && gridEveromentUserList.size()>0){
                                gridEveromentUser.setMid(gridEveromentUserList.get(0).getMid());
                                gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);

                        }else {
                            gridEveromentUserMapper.insertSelective(gridEveromentUser);
                        }*/
                    }
                }
                json.setObj(gridEveromentUserList);
                json.setFlag(0);
                json.setMsg("ok");
            }else {
                //查询 输入时间那天  社区人员的签到情况
                Map<String, Object> map3 = new HashMap<String, Object>();

                PageParams pageParams=new PageParams();
                pageParams.setPage(page);
                pageParams.setPageSize(pageSize);
                pageParams.setUseFlag(useFlag);
                map3.put("page",pageParams);
                map3.put("gridId",deptId);
                map3.put("date",date);
                List<GridEveromentUser> gridEveromentUserList1 = gridEveromentUserMapper.selectEveromentUser(map3);
                Map<String, Object> map2 = new HashMap<String, Object>();
                map2.put("gridId",deptId);
                map2.put("date",date);
                json.setObj(gridEveromentUserList1);
                json.setTotleNum(gridEveromentUserMapper.queryCount(map2));
                json.setFlag(0);
                json.setMsg("ok");
            }


        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }


    public ToJson<GridEveromentUser> selectCheck(String time, String key, Integer deptId, Integer page, Integer pageSize,
                                                 boolean useFlag, String isPage){
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            Integer begin = DateFormat.getDateTime(time);// 开始时间戳
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(time);
            Calendar calendar  =  new GregorianCalendar();
            calendar.setTime(date);
            calendar.add(calendar.DATE,1);//把日期往后增加一天.整数往后推,负数往前移动
            date=calendar.getTime();   //这个时间就是日期往后推一天的结果
            String T = DateFormat.getDatestr(date);
            Integer end = DateFormat.getDateTime(T); // 结束时间戳
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("begin",begin);
            map.put("end",end);
            Map<String, Object> map1 = new HashMap<String, Object>();
            map1.put("newTime",time);
            //查询前端传入时间是否在节假日范围内
            List<GridHoliday> gridHolidayList =gridHolidayMapper.selectHoliday(map1);
            GridEveromentUser gridEveromentUser = null;
            Date date1=sdf.parse(time);

            List<Users> usersList = gridEveromentUserMapper.selectUser(deptId);
            List<GridEveromentUser> gridEveromentUserList = new ArrayList<GridEveromentUser>();
            if (usersList!=null && usersList.size()>0){
                for (Users user:usersList){
                    gridEveromentUser = new GridEveromentUser();
                    gridEveromentUser.setGridId(user.getDeptId().toString());
                    gridEveromentUser.setGridName(user.getDeptName());
                    gridEveromentUser.setGridUserId(user.getUserId());
                    gridEveromentUser.setGridUserName(user.getUserName());
                    gridEveromentUser.setDate(date1);
                    map.put("uid",user.getUid());
                   // List<Foot> footList = footPrintMapper.getUserFootPrint(map);
                    //判断 输入时间  那天  有无签到
                   /* if (footList!=null && footList.size()>0){
                        Integer fTime=footList.get(0).getUpdateTime();
                        Long l=(new Long(fTime.toString()))*1000; ;
                        Date d = new Date(l);
                        String res = sdf.format(d);
                        gridEveromentUser.setAttendNum(res);
                        gridEveromentUser.setAttendScore(footList.get(0).getLocation());
                        if("0".equals(key)){
                            gridEveromentUser.setM1("0");
                        }else if (gridHolidayList !=null && gridHolidayList.size()>0){
                            gridEveromentUser.setM1("0");
                        }else {
                            gridEveromentUser.setM1("1");
                        }
                    }else {
                        gridEveromentUser.setM1("0");
                    }*/
                    gridEveromentUserList.add(gridEveromentUser);
//                    Map<String, Object> map2 = new HashMap<String, Object>();
//                    map2.put("date",date1);
//                    map2.put("gridUserId",user.getUserId());
//                    //  查询输入时间  是否在环保考核人员表里有记录
//                    //有就执行更新操作  没有就新建
//                    List<GridEveromentUser> gridEveromentUserList = gridEveromentUserMapper.selectEveromentUser(map2);
//                    if(gridEveromentUserList!=null && gridEveromentUserList.size()>0){
//                        gridEveromentUser.setMid(gridEveromentUserList.get(0).getMid());
//                        gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
//
//                    }else {
//                        gridEveromentUserMapper.insertSelective(gridEveromentUser);
//                    }
                }
            }
//            //查询 输入时间那天  社区人员的签到情况
//            Map<String, Object> map3 = new HashMap<String, Object>();
//            if ("1".equals(isPage)){
//                map3.put("gridId",deptId);
//                map3.put("date",date1);
//                List<GridEveromentUser> gridEveromentUserList1 = gridEveromentUserMapper.selectEveromentUser(map3);
//                json.setObj(gridEveromentUserList1);
//                json.setTotleNum(gridEveromentUserMapper.queryCount(map3));
//            }else {
//                PageParams pageParams=new PageParams();
//                pageParams.setPage(page);
//                pageParams.setPageSize(pageSize);
//                pageParams.setUseFlag(useFlag);
//                map3.put("page",pageParams);
//                map3.put("gridId",deptId);
//                map3.put("date",date1);
//                List<GridEveromentUser> gridEveromentUserList1 = gridEveromentUserMapper.selectEveromentUser(map3);
//                json.setObj(gridEveromentUserList1);
//                json.setTotleNum(gridEveromentUserMapper.queryCount(map3));
//            }

            json.setObj(gridEveromentUserList);
            json.setFlag(0);
            json.setMsg("ok");
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   张丽军
     * 创建日期:   2018-1-12 下午15:26:51
     * 类介绍  :  巡查记录统计接口(根据社区id查询网格员和巡查记录数、分数)
     * 构造参数:gridEveromentUser,deptId,date
     *
     */
    public ToJson<GridEveromentUser> getCheckCountNum(HttpServletRequest request, GridEveromentUser gridEveromentUser, Integer deptId, String time, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(time);
            if(time.equals(DateFormat.getCurrentTime2())){
                List<GridEveromentUser> gridEveromentUserList = new ArrayList<>();
                       //查询对应社区下人员名称
                        List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(deptId);
                        int sum=0;
                        if(list!=null){
                            for (OrgGoviment orgGoviment:list){
                                //获取巡查统计数
                                HashMap<String, Object> map = new HashMap<String, Object>();
                                map.put("deptId",deptId);
                                map.put("userId",orgGoviment.getUserId());
                                map.put("checkTime",time);
                                int coun =orgGovimentMapper.getCheckCount(map);
                                //获取分数
                                sum=coun;
                                //保存数据存放表中
                                GridEveromentUser gridEveromentUser1 = new GridEveromentUser();
                                gridEveromentUser1.setGridId(String.valueOf(orgGoviment.getDeptId()));
                                gridEveromentUser1.setGridName(orgGoviment.getDeptName());
                                gridEveromentUser1.setDate(date);
                                gridEveromentUser1.setGridUserId(orgGoviment.getUserId());
                                gridEveromentUser1.setGridUserName(orgGoviment.getUserName());
                                gridEveromentUser1.setCheckRecordNum(String.valueOf(coun));
                                gridEveromentUser1.setCheckScore(String.valueOf(sum));

                                gridEveromentUserList.add(gridEveromentUser1);
                                /*Map<String, Object> map1 = new HashMap<String, Object>();
                                map.put("gridId",deptId);
                                map1.put("gridUserId",gridEveromentUser.getGridUserId());
                                map1.put("date",date);
                                //  查询输入时间  是否在环保考核人员表里有记录
                                //有就执行更新操作  没有就新建
                                List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                                if(list1!=null && list1.size()>0){
                                    gridEveromentUser.setMid(list1.get(0).getMid());
                                    gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                                }
                                else {
                                    gridEveromentUserMapper.insertSelective(gridEveromentUser);
                                }*/
                            }
                        }
                        json.setObj(gridEveromentUserList);
                        json.setFlag(0);
                        json.setMsg("ok");
            }else {

                //分页
                PageParams pageParams = new PageParams();
                pageParams.setPage(page);
                pageParams.setPageSize(pageSize);
                pageParams.setUseFlag(useFlag);
                HashMap<String,Object> map=new HashMap<>();
                map.put("page", pageParams);
                map.put("gridEveromentUser",gridEveromentUser);
                map.put("gridId",deptId);
                map.put("date",date);
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("gridId",deptId);
                map1.put("date",date);
                List<GridEveromentUser> list2=gridEveromentUserMapper.selectEveromentUser(map);
                if (list2!=null){
                    json.setObj(list2);
                    json.setTotleNum(gridEveromentUserMapper.selectCountNum(map1));
                    json.setFlag(0);
                    json.setMsg("ok");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     * 定时添加巡查记录接口(根据社区id查询网格员和巡查记录数、分数)
     * @return
     */
    public ToJson<GridEveromentUser> getCheckCountNumInsert() {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            //查询社区列表
            List<Department> list0 = departmentMapper.getChDept(27);
            for (Department department:list0){
                Date d1 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(d1);
                /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = "2018-01-25";*/
                Date date=sdf.parse(time);
                //查询对应社区下人员名称
                List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(department.getDeptId());
                int sum=0;
                if(list!=null){
                    for (OrgGoviment orgGoviment:list){
                        //获取巡查统计数
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("deptId",department.getDeptId());
                        map.put("userId",orgGoviment.getUserId());
                        map.put("checkTime",time);
                        int coun =orgGovimentMapper.getCheckCount(map);
                        //获取分数
                        sum=coun;
                        //保存数据存放表中
                        GridEveromentUser gridEveromentUser=new GridEveromentUser();
                        gridEveromentUser.setGridId(String.valueOf(orgGoviment.getDeptId()));
                        gridEveromentUser.setGridName(orgGoviment.getDeptName());
                        gridEveromentUser.setDate(date);
                        gridEveromentUser.setGridUserId(orgGoviment.getUserId());
                        gridEveromentUser.setGridUserName(orgGoviment.getUserName());
                        gridEveromentUser.setCheckRecordNum(String.valueOf(coun));
                        gridEveromentUser.setCheckScore(String.valueOf(sum));

                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map.put("gridId",department.getDeptId());
                        map1.put("gridUserId",gridEveromentUser.getGridUserId());
                        map1.put("date",time);
                        //  查询输入时间  是否在环保考核人员表里有记录
                        //有就执行更新操作  没有就新建
                        List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                        if(list1!=null && list1.size()>0){
                            gridEveromentUser.setMid(list1.get(0).getMid());
                            gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                        }
                    else {
                        gridEveromentUserMapper.insertSelective(gridEveromentUser);
                    }
                    }
                }
            }
                json.setFlag(0);
                json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    public ToJson<GridEveromentUser> getCheckCountNumInsert1(String DateTime) {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            //查询社区列表
            List<Department> list0 = departmentMapper.getChDept(27);
            for (Department department:list0){
//                Date d1 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//                String time = sdf.format(d1);
                /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = "2018-01-25";*/
                Date date=sdf.parse(DateTime);
                //查询对应社区下人员名称
                List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(department.getDeptId());
                int sum=0;
                if(list!=null){
                    for (OrgGoviment orgGoviment:list){
                        //获取巡查统计数
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("deptId",department.getDeptId());
                        map.put("userId",orgGoviment.getUserId());
                        map.put("checkTime",DateTime);
                        int coun =orgGovimentMapper.getCheckCount(map);
                        //获取分数
                        sum=coun;
                        //保存数据存放表中
                        GridEveromentUser gridEveromentUser=new GridEveromentUser();
                        gridEveromentUser.setGridId(String.valueOf(orgGoviment.getDeptId()));
                        gridEveromentUser.setGridName(orgGoviment.getDeptName());
                        gridEveromentUser.setDate(date);
                        gridEveromentUser.setGridUserId(orgGoviment.getUserId());
                        gridEveromentUser.setGridUserName(orgGoviment.getUserName());
                        gridEveromentUser.setCheckRecordNum(String.valueOf(coun));
                        gridEveromentUser.setCheckScore(String.valueOf(sum));

                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map.put("gridId",department.getDeptId());
                        map1.put("gridUserId",gridEveromentUser.getGridUserId());
                        map1.put("date",DateTime);
                        //  查询输入时间  是否在环保考核人员表里有记录
                        //有就执行更新操作  没有就新建
                        List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                        if(list1!=null && list1.size()>0){
                            gridEveromentUser.setMid(list1.get(0).getMid());
                            gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                        }
                        else {
                            gridEveromentUserMapper.insertSelective(gridEveromentUser);
                        }
                    }
                }
            }
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   张丽军
     * 创建日期:   2018-1-12 下午15:26:51
     * 类介绍  :  网格中心统计接口(根据社区id查询网格员和上报网络中心、分数)
     * 构造参数:gridEveromentUser,deptId,date
     *
     */

    public ToJson<GridEveromentUser> getEventCountNum(HttpServletRequest request, GridEveromentUser gridEveromentUser, Integer deptId, String time, Integer page, Integer pageSize, boolean useFlag) {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {

            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            Date date=sdf.parse(time);
            if(time.equals(DateFormat.getCurrentTime2())){
                    List<GridEveromentUser> gridEveromentUserList = new ArrayList<>();
                    //查询对应社区下人员名称
                    List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(deptId);
                    int sum=0;
                    if(list!=null){
                        for (OrgGoviment orgGoviment:list){
                            //获取上报网格数
                            HashMap<String, Object> map = new HashMap<String, Object>();
                            map.put("deptId",deptId);
                            map.put("userId",orgGoviment.getUserId());
                            map.put("eventTime",time);
                            int coun =gridEventMapper.getEventCountByUserId(map);
                            //获取网格分数
                            sum=coun * 2;
                            //保存数据存放表中
                            GridEveromentUser gridEveromentUser1 = new GridEveromentUser();
                            gridEveromentUser1.setGridId(String.valueOf(orgGoviment.getDeptId()));
                            gridEveromentUser1.setGridName(orgGoviment.getDeptName());
                            gridEveromentUser1.setDate(date);
                            gridEveromentUser1.setGridUserId(orgGoviment.getUserId());
                            gridEveromentUser1.setGridUserName(orgGoviment.getUserName());
                            gridEveromentUser1.setReportNum(String.valueOf(coun));
                            gridEveromentUser1.setRepoetScore(String.valueOf(sum));

                            gridEveromentUserList.add(gridEveromentUser1);
                            /*Map<String, Object> map1 = new HashMap<String, Object>();
                            map1.put("gridId",deptId);
                            map1.put("gridUserId",gridEveromentUser.getGridUserId());
                            map1.put("date",date);
                            //  查询输入时间  是否在环保考核人员表里有记录
                            //有就执行更新操作  没有就新建
                            List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                            if(list1!=null && list1.size()>0){
                                gridEveromentUser.setMid(list1.get(0).getMid());
                                gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                            }
                            else {
                                gridEveromentUserMapper.insertSelective(gridEveromentUser);
                            }*/
                        }
                    }
                    json.setObj(gridEveromentUserList);
                    json.setFlag(0);
                    json.setMsg("ok");

            }else {
                //分页
                PageParams pageParams = new PageParams();
                pageParams.setPage(page);
                pageParams.setPageSize(pageSize);
                pageParams.setUseFlag(useFlag);
                HashMap<String,Object> map=new HashMap<>();
                map.put("page", pageParams);
                map.put("gridEveromentUser",gridEveromentUser);
                map.put("gridId",deptId);
                map.put("date",date);
                Map<String, Object> map1 = new HashMap<String, Object>();
                map1.put("gridId",deptId);
                map1.put("date",date);
                List<GridEveromentUser> list2=gridEveromentUserMapper.selectEveromentUser(map);
                if (list2!=null){
                    json.setObj(list2);
                    json.setTotleNum(gridEveromentUserMapper.selectCountNum(map1));
                    json.setFlag(0);
                    json.setMsg("ok");
                }
            }

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }

    /**
     * 定时添加网格中心统计接口(根据社区id查询网格员和上报网络中心、分数)
     * @return
     */
    public ToJson<GridEveromentUser> getEventCountNumInsert() {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            //查询社区列表
            List<Department> list0 = departmentMapper.getChDept(27);
            for(Department department:list0){
                Date d1 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = sdf.format(d1);
                /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = "2018-01-25";*/
                Date date=sdf.parse(time);
                //查询对应社区下人员名称
                List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(department.getDeptId());
                int sum=0;
                if(list!=null){
                    for (OrgGoviment orgGoviment:list){
                        //获取上报网格数
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("deptId",department.getDeptId());
                        map.put("userId",orgGoviment.getUserId());
                        map.put("eventTime",time);
                        int coun =gridEventMapper.getEventCountByUserId(map);
                        //获取网格分数
                        sum=coun * 2;
                        //保存数据存放表中
                        GridEveromentUser gridEveromentUser=new GridEveromentUser();
                        gridEveromentUser.setGridId(String.valueOf(orgGoviment.getDeptId()));
                        gridEveromentUser.setGridName(orgGoviment.getDeptName());
                        gridEveromentUser.setDate(date);
                        gridEveromentUser.setGridUserId(orgGoviment.getUserId());
                        gridEveromentUser.setGridUserName(orgGoviment.getUserName());
                        gridEveromentUser.setReportNum(String.valueOf(coun));
                        gridEveromentUser.setRepoetScore(String.valueOf(sum));

                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map1.put("gridId",department.getDeptId());
                        map1.put("gridUserId",gridEveromentUser.getGridUserId());
                        map1.put("date",time);
                        //  查询输入时间  是否在环保考核人员表里有记录
                        //有就执行更新操作  没有就新建
                        List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                        if(list1!=null && list1.size()>0){
                            gridEveromentUser.setMid(list1.get(0).getMid());
                            gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                        }
                        else {
                            gridEveromentUserMapper.insertSelective(gridEveromentUser);
                        }
                    }
                }
            }
                json.setFlag(0);
                json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    public ToJson<GridEveromentUser> getEventCountNumInsert1(String DateTime) {
        ToJson<GridEveromentUser> json = new ToJson<>();
        try {
            //查询社区列表
            List<Department> list0 = departmentMapper.getChDept(27);
            for(Department department:list0){
   //             Date d1 = new Date();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
  //              String time = sdf.format(d1);
                /*SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                String time = "2018-01-25";*/
                Date date=sdf.parse(DateTime);
                //查询对应社区下人员名称
                List<OrgGoviment> list =orgGovimentMapper.orgGovimentUser(department.getDeptId());
                int sum=0;
                if(list!=null){
                    for (OrgGoviment orgGoviment:list){
                        //获取上报网格数
                        HashMap<String, Object> map = new HashMap<String, Object>();
                        map.put("deptId",department.getDeptId());
                        map.put("userId",orgGoviment.getUserId());
                        map.put("eventTime",DateTime);
                        int coun =gridEventMapper.getEventCountByUserId(map);
                        //获取网格分数
                        sum=coun * 2;
                        //保存数据存放表中
                        GridEveromentUser gridEveromentUser=new GridEveromentUser();
                        gridEveromentUser.setGridId(String.valueOf(orgGoviment.getDeptId()));
                        gridEveromentUser.setGridName(orgGoviment.getDeptName());
                        gridEveromentUser.setDate(date);
                        gridEveromentUser.setGridUserId(orgGoviment.getUserId());
                        gridEveromentUser.setGridUserName(orgGoviment.getUserName());
                        gridEveromentUser.setReportNum(String.valueOf(coun));
                        gridEveromentUser.setRepoetScore(String.valueOf(sum));

                        Map<String, Object> map1 = new HashMap<String, Object>();
                        map1.put("gridId",department.getDeptId());
                        map1.put("gridUserId",gridEveromentUser.getGridUserId());
                        map1.put("date",DateTime);
                        //  查询输入时间  是否在环保考核人员表里有记录
                        //有就执行更新操作  没有就新建
                        List<GridEveromentUser> list1=gridEveromentUserMapper.selectEveromentUser(map1);
                        if(list1!=null && list1.size()>0){
                            gridEveromentUser.setMid(list1.get(0).getMid());
                            gridEveromentUserMapper.updateByPrimaryKeySelective(gridEveromentUser);
                        }
                        else {
                            gridEveromentUserMapper.insertSelective(gridEveromentUser);
                        }
                    }
                }
            }
            json.setFlag(0);
            json.setMsg("ok");

        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
